package com.wgw.first.user.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.wgw.first.R;
import com.wgw.first.user.bean.OrderBean2;
import com.wgw.first.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class ReceiveOrderActivity extends Activity {

    private ListView lv_order;
    private OrderBean2 orderBean2;
    public  Boolean status = true;
    private List<OrderBean2.Order> receive_order = new ArrayList<OrderBean2.Order>();
    private List<OrderBean2.Order> all_order;
    private int order_id;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_receive_order);
        lv_order = (ListView) findViewById(R.id.lv_order);
//        OrderAdapter orderAdapter = new OrderAdapter(ReceiveOrderActivity.this);
//        lv_order.setAdapter(orderAdapter);
        getDataFromNet();
    }
    public void getDataFromNet() {
        OkHttpUtils
                .get()
                .addParams("username", Constants.ALEADYLOGIN)
                .url(Constants.BASE+Constants.DOWNORDERDATA)
                //.url("http://192.168.191.1:8080/Product/HomeServlet")
                .id(100)
                .build()
                .execute(new MyStringCallback());
    }
    public class MyStringCallback extends StringCallback {

        @Override
        public void onError(Call call, Exception e, int id) {
            Log.e("TAG", "联网失败" + e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {

            switch (id) {
                case 100:
                    if (response != null) {
                        System.out.println("下载下来的订单数据：" + response);
                        //如果response为空，就不解析了
                        //对response进行json解析
                        int a = response.indexOf("{");
                        int b = response.lastIndexOf("}");
                        System.out.println("a:"+a);
                        System.out.println("b:"+b);
                        System.out.println("111" + response.substring(a+1,b).trim());
                        Boolean boo = !response.substring(a+1,b).trim().equals("");
                        System.out.println(boo);
                        if(!response.substring(a+1,b).trim().equals("")){
                            processData(response);
                            all_order =orderBean2.getOrder();
                            for(int i=0;i<all_order.size();i++){
                                if(all_order.get(i).getOrder_status()==2){
                                    receive_order.add(all_order.get(i));
                                }
                            }

                            ReceiveOrderAdapter receiveOrderAdapter = new ReceiveOrderAdapter(receive_order,ReceiveOrderActivity.this);
                            lv_order.setAdapter(receiveOrderAdapter);


                        }else{
                            status = false;
                        }

                    }
                    break;
                case 101:
                    //                    Toast.makeText(mContext, "https", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    private void processData(String json) {

        if (!TextUtils.isEmpty(json)) {
            JSONObject OrderBean2 = JSON.parseObject(json, com.wgw.first.user.bean.OrderBean2.class);

            String order = OrderBean2.getString("order");

            orderBean2 = new OrderBean2();

            List<OrderBean2.Order> orders = JSON.parseArray(order, com.wgw.first.user.bean.OrderBean2.Order.class);
            orderBean2.setOrder(orders);

            System.out.println(orderBean2.toString());
        }

    }


}
class ReceiveOrderAdapter extends BaseAdapter {

    private Context mcontext;
    private List<OrderBean2.Order> orders;
    private int order_id;

    public ReceiveOrderAdapter(List<OrderBean2.Order> orders, Context context){
        this.orders = orders;
        this.mcontext = context;
    }

    @Override
    public int getCount() {
        return orders.size();
    }

    @Override
    public Object getItem(int position) {
        return orders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = View.inflate(mcontext, R.layout.item_order_receive,null);
        final OrderBean2.Order order = (OrderBean2.Order) getItem(position);

        ImageView iv_gov = (ImageView)v.findViewById(R.id.iv_gov);
        TextView tv_desc_gov = (TextView)v.findViewById(R.id.tv_desc_gov);
        TextView  tv_address = (TextView)v.findViewById(R.id.tv_address);
        TextView  tv_phone = (TextView)v.findViewById(R.id.tv_phone);
        TextView  tv_count = (TextView)v.findViewById(R.id.tv_total_number);
        TextView  tv_totalprice = (TextView)v.findViewById(R.id.tv_totalprice);
        TextView  tv_date = (TextView)v.findViewById(R.id.tv_date);
        Button bt_receive = (Button)v.findViewById(R.id.bt_receive);
        Glide.with(mcontext)
                .load(Constants.BASE+Constants.IMG+order.getImgUrl())
                .into(iv_gov);

        tv_desc_gov.setText(order.getBook_name());
        tv_address.setText("地址："+order.getAddress());
        tv_phone.setText("电话："+order.getPhone());
        tv_count.setText("数量：" + order.getNumber());
        tv_totalprice.setText("￥" + order.getTotal_price());
        tv_date.setText("下单时间:"+order.getDate());

        bt_receive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mcontext, "确认收货成功", Toast.LENGTH_SHORT).show();
                orders.remove(position);
                notifyDataSetChanged();
                order_id = order.getOrder_id();
                System.out.println("receive orderid:"+order_id);
                getDataFromNet();

            }
        });



        return v;
    }
    public void getDataFromNet() {
        OkHttpUtils
                .get()
                .addParams("orderID",String.valueOf(order_id))
                .url(Constants.BASE+Constants.RECEIVESERVLET)
                //.url("http://192.168.191.1:8080/Product/HomeServlet")
                .id(100)
                .build()
                .execute(new MyStringCallback());
    }
    public class MyStringCallback extends StringCallback {

        @Override
        public void onError(Call call, Exception e, int id) {
            Log.e("TAG", "联网失败" + e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {

            switch (id) {
                case 100:
                    if (response != null) {


                    }
                    break;
                case 101:
                    //                    Toast.makeText(mContext, "https", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
}
